package view2.utils;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zhinongbao.R;
import com.example.zhinongbao.contract.SettingContact;
import com.example.zhinongbao.view.AboutSoftware;
import com.example.zhinongbao.view.AboutUs;
import com.example.zhinongbao.view.FeedbackActivity;
import com.example.zhinongbao.view.PersonalCenter;
import com.example.zhinongbao.view.UpdateLoginPassword;
import com.example.zhinongbao.view.UpdatePayPassword;


public class Setting extends AppCompatActivity implements View.OnClickListener , SettingContact {
    private ImageButton setBackBtn ;
    private LinearLayout setPersonMessage , setLocalManage , setLoginPaw , setPayPaw ;
    private LinearLayout setCallCustomer , setFeedback , setAboutUs , setAboutSoftware ;
    private Button setSignOut ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        setBackBtn = findViewById(R.id.set_back_btn);
        setPersonMessage = findViewById(R.id.setting_person_message) ;
        setLocalManage = findViewById(R.id.setting_local_manager) ;
        setLoginPaw = findViewById(R.id.setting_login_password) ;
        setPayPaw = findViewById(R.id.setting_pay_password) ;
        setCallCustomer = findViewById(R.id.setting_call_customer) ;
        setFeedback = findViewById(R.id.setting_feedback) ;
        setAboutUs = findViewById(R.id.setting_about_us) ;
        setAboutSoftware = findViewById(R.id.setting_about_software);
        setSignOut = findViewById(R.id.setting_sign_out);

        onSetBackBtnClick();
        onSetPersonMsgBtnClick();
        onSetLocalMsgBtnClick();
        onSetLoginPasswordBtnClick();
        onSetPayPasswordBtnClick();
        onSetCallCustomerBtnClick();
        onSetFeedbackBtnClick();
        onSetAboutUsBtnClick();
        onSetAboutSoftwareBtnClick();
        onSetSignOutBtnClick();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.set_back_btn){
            onBackPressed();
        } else if (v.getId() == R.id.setting_person_message) {
            Intent intent = new Intent(this , PersonalCenter.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_local_manager) {
            Toast.makeText(this, "地址选择功能未开发", Toast.LENGTH_SHORT).show();
        }else if (v.getId() == R.id.setting_login_password){
            Intent intent = new Intent(this , UpdateLoginPassword.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_pay_password) {
            Intent intent = new Intent(this , UpdatePayPassword.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_call_customer) {
            Toast.makeText(this, "电话客服功能未开发", Toast.LENGTH_SHORT).show();
        } else if (v.getId() == R.id.setting_feedback) {
            Intent intent = new Intent(this , FeedbackActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_about_us) {
            Intent intent = new Intent(this , AboutUs.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_about_software) {
            Intent intent = new Intent(this , AboutSoftware.class) ;
            startActivity(intent);
        } else if (v.getId() == R.id.setting_sign_out) {
            Toast.makeText(this, "退出登录功能未开发", Toast.LENGTH_SHORT).show();
        }
    }

//    public void deleteUserDataFromDB() {
//        // 假设您有一个SQLiteOpenHelper的子类叫做MyDatabaseHelper
//        DBHelper dbHelper = new DBHelper(@Nullable Context );
//        SQLiteDatabase db = dbHelper.getWritableDatabase();
//
//        // 定义要删除的用户的ID，这里我们假设用户ID存储在SharedPreferences中
//        SharedPreferences preferences = Context.getSharedPreferences("UserPreferences", Context.MODE_PRIVATE);
//        String userId = preferences.getString("userId", null);
//
//        // 检查用户ID是否存在
//        if (userId != null) {
//            // 开启事务
//            db.beginTransaction();
//
//            try {
//                // 执行删除操作
//                // 这里"users"是假设的用户表名，"user_id"是表中的用户ID字段
//                int rowsDeleted = db.delete("users", "user_id = ?", new String[] { userId });
//
//                // 如果删除了行，提交事务
//                if (rowsDeleted > 0) {
//                    db.setTransactionSuccessful();
//                    // 可以在这里添加额外的清理工作，例如清除SharedPreferences中的用户数据
//                    SharedPreferences.Editor editor = preferences.edit();
//                    editor.remove("userId");
//                    editor.apply();
//                }
//            } catch (Exception e) {
//                // 发生错误时回滚事务
//                db.endTransaction();
//                e.printStackTrace();
//            } finally {
//                // 事务结束，关闭数据库
//                db.endTransaction();
//                db.close();
//            }
//        }
//    }




    @Override
    public void onSetBackBtnClick() {
        setBackBtn.setOnClickListener(this);
    }

    @Override
    public void onSetPersonMsgBtnClick() {
        setPersonMessage.setOnClickListener(this);
    }

    @Override
    public void onSetLocalMsgBtnClick() {
        setLocalManage.setOnClickListener(this);
    }

    @Override
    public void onSetLoginPasswordBtnClick() {
        setLoginPaw.setOnClickListener(this);
    }

    @Override
    public void onSetPayPasswordBtnClick() {
        setPayPaw.setOnClickListener(this);
    }

    @Override
    public void onSetCallCustomerBtnClick() {
        setCallCustomer.setOnClickListener(this);
    }

    @Override
    public void onSetFeedbackBtnClick() {
        setFeedback.setOnClickListener(this);
    }

    @Override
    public void onSetAboutUsBtnClick() {
        setAboutUs.setOnClickListener(this);
    }

    @Override
    public void onSetAboutSoftwareBtnClick() {
        setAboutSoftware.setOnClickListener(this);
    }

    @Override
    public void onSetSignOutBtnClick() {
        setSignOut.setOnClickListener(this);
    }
}
